const base64 = require('@/common/base64.js')
/**
 * 左截取文本
 */
export function leftEllipsis(str, count = 10, char = '') {
	let result = str;
	const len = str?.length || 0;
	if (len > count) {
		result = `${str.slice(0, count)}${char}`;
	}

	return result;
}
/**
 * 右截取
 */
export function rightEllipsis(str, count = 10, ) {
	if (str) {
		return str.toString().substring(str.length - count)
	}
	return ''
}
/**
 * base64 解码
 * @param {Object} val
 */
export function Base64Decode(val) {
	return base64.decode(val)
}

function path(list = [], id) {
	if (!list.length) {
		return
	}
	if (!id) {
		return
	}
	let path = []
	let findResult = {}
	let PID = 0;
	let Num = 0
	findResult = list.find((item) => item.id == id)
	PID = findResult?.parent_id || 0
	path.push(findResult)
	Num += 1
	if (PID) {
		for (let i = 0; i < Num; i++) {
			findResult = list.find((item) => item.id == PID);
			PID = findResult?.parent_id || 0
			if (PID) {
				path.push(findResult)
				Num += 1
			} else {
				path.push(findResult)
				break
			}

		}
	}
	return path.reverse()
}

/**
 * 故障分类路径
 */
export function faultTypeNamePath(val) {
	let value = ''
	if (typeof val === 'string') {
		value = val.split(',')
		value = value[value.length - 1]
	} else if (val instanceof Array) {
		value = val[val.length - 1]
	} else if (value == undefined) {
		value = ''
	} else {
		value = val
	}



	var list = uni.getStorageSync('dic').faultClassification
	let arr = path(list, value)
	if (arr instanceof Array && arr.length) {
		let namePath = arr.map(e => e?.name || '').join('/')
		return namePath
	}
	return ''
}

export function deleteHTMLTag(htmlStr) {
	if (!htmlStr) {
		return '';
	}
	const REG_STYLE = new RegExp('<style[^>]*?>[\\s\\S]*?<\\/style>', 'g');
	const REG_HTML = new RegExp('<[^>]+>', 'g');
	const resHtmlStr = htmlStr.replace(REG_STYLE, '').replace(REG_HTML, '');
	if (resHtmlStr.indexOf('图片：<img') === -1) {
		return resHtmlStr.split('图片：<img')[0];
	} else {
		return resHtmlStr.split('图片：')[0] || resHtmlStr.split('图片：')[2] || resHtmlStr.split('图片：')[1];
	}
};


/**
 * 获取字典值
 * @param {} dicKey 
 */
export function dicList(dicKey = '') {
	if (!dicKey) {
		throw Error('dicKey为空')
	}
	let dic = wx.getStorageSync('dic')

	return dic[dicKey]
}

function sum(val) {
	return val.reduce((a, b) => Number(a || 0) + Number(b || 0))
}

export function mathSum(val) {
	if (val) {
		if (typeof val === 'string') {
			val = val.split(',')
			return sum(val)
		}
		if (val instanceof Array) {
			return sum(val)
		}
	}
	return 0
}


function dicKeyToName(key, value) {
	if (value == null || value == '' || value == undefined) {
		console.error(`dicKeyToName value is ${value}！`)
		return '-'
	}
	var list = dicList(key)
	let splitVal = value.toString().split(',')
	let result = ''
	if (splitVal.length > 1) {
		let res = list.filter((item) => splitVal.indexOf(item.value) > -1)
		let names = res.map((item) => item.name)
		result = names.join(',')
	} else {

		let res = list.find((item) => splitVal.indexOf(item.value) > -1)
		result = res?.name || '-'
	}
	return result
}

export function itemProcessShow(value) {
	return dicKeyToName('itemProcessShow', value)
}

export function itemLevel(value) {
	return dicKeyToName('itemLevel', value)

}
export function itemProjectLevel(value) {
	return dicKeyToName('itemProjectLevel', value)
}

/**
 * 项目优先级
 */
export function itemPriority(value) {
	return dicKeyToName('itemPriority', value)
}
/**
 * 项目类型
 * 
 */

export function projectType(value) {
	return dicKeyToName('projectType', value)
}
/**
 * 涉及系统
 */
export function aboutSystem(value) {
	return dicKeyToName('aboutSystem', value)
}
/**
 * 需求类型
 */
export function itemType(value) {
	return dicKeyToName('itemType', value)
}
/**
 * 涉及业务领域
 */
export function coverageInvolved(value) {
	return dicKeyToName('coverageInvolved', value)
}

/**
 * 实施形式
 */
export function implementation_form(value) {
	return dicKeyToName('implementation_form', value)
}
/**
 * 难易程度
 */
export function difficulty(value) {
	return dicKeyToName('difficulty', value)
}
/**
 * 显示星期几
 */
export function weekDay(time = null) {
	const weekNum = new Date(time).getDay();
	let week = "";
	switch (weekNum) {
		case 0:
			week = "星期天";
			break;
		case 1:
			week = "星期一";
			break;
		case 2:
			week = "星期二";
			break;
		case 3:
			week = "星期三";
			break;
		case 4:
			week = "星期四";
			break;
		case 5:
			week = "星期五";
			break;
		case 6:
			week = "星期六";
			break;
	}
	return week;
};

/**
 * 处理用户名单人
 */
export function user(value) {
	var list = uni.getStorageSync('user')
	return list[value] || ''
}
/**
 * 处理用户名多人
 */
export function users(value) {
	value = '' + value
	if (!value) {
		var name = ""
	} else {
		var users = value.split(',')
		var list = uni.getStorageSync('user')
		var array = []
		users.forEach(item => {
			array.push(list[item])
		})

		var name = array.join(',')
	}

	return name
}
